package com.cw.controller;

import com.cw.pojo.Dept;
import com.cw.pojo.Result;
import com.cw.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptService deptService;//面向接口编程
    @GetMapping
    public Result list(){
        log.info("查询全部部门数据");
        List<Dept> deptList = deptService.list(); //此处查询出来是多个对象--集合
        return Result.success(deptList);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) throws Exception {
        log.info("根据id删除部门:{}",id);
        deptService.delete(id);
        return Result.success();
    }

    //新增部门数据
    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("新增部门");
        deptService.add(dept);
        return Result.success();
    }

    //修改部门数据
    //1.根据ID查询
    //2.修改部门
    @GetMapping("/{id}")
    public Result select(@PathVariable Integer id){
        log.info("根据ID查询部门{}",id);
        Dept dept = deptService.select(id); //此处查询出来是单个对象
        return Result.success(dept);
    }

    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("修改部门:{}",dept);
        deptService.update(dept);
        return Result.success();
    }

}
